package login;

import javax.ejb.EJB;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

import users.Usuario;
import users.UsuarioEJB;


public class UsuarioBean {
	@EJB
	UsuarioEJB userEjb;
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login(ActionEvent actionEvent) throws Throwable {  
        RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;  
        boolean loggedIn = false; 
        boolean esUser=true;
        Usuario userJpaPrueba;
        userJpaPrueba = userEjb.autentificacion(username, password);
        
        if(userJpaPrueba !=null) {  
            loggedIn = true;  
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
            FacesContext.getCurrentInstance().addMessage(null, msg);  
            context.addCallbackParam("loggedIn", loggedIn);  
            return "/Sig_Web_Interface/pages/Login.jsf";
        } else {  
            loggedIn = false;  
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");  
            FacesContext.getCurrentInstance().addMessage(null, msg);  
            context.addCallbackParam("loggedIn", loggedIn);  
            return null;
        }
       
        
    }
}
